
import air.update.ApplicationUpdaterUI;

import flash.display.StageDisplayState;

import flextv.core.event.PlayItemEvent;
import flextv.core.logic.CoreModel;
import flextv.core.logic.Logger;
import flextv.core.vo.IPlayable;
import flextv.core.vo.StoredItem;
import flextv.display.event.PlayStateEvent;
import flextv.display.event.ToggleFullscreenEvent;
import flextv.display.logic.DisplayModel;
import flextv.main.logic.MainModel;
import flextv.main.logic.MainServices;
import flextv.main.logic.SpringApplicationContext;
import flextv.main.view.PlaylistGrid;

import mx.core.Application;
import mx.logging.ILogger;

// use namespace mx_internal;

private static const LOG: ILogger = Logger.getLog(FlexTV);

/*private function onUnhandledError(event: *): void {
	trace("x");
}*/

private static var appUpdater: ApplicationUpdaterUI;

private function onInitialize(): void {
	
	//UIComponentGlobals.catchCallLaterExceptions = true;
	//Application.application.systemManager.addEventListener(ErrorEvent.ERROR, onUnhandledError);
	
	// --------------------------------------------
	// INIT UPDATER
	// --------------------------------------------
	appUpdater = new ApplicationUpdaterUI();
	appUpdater.configurationFile = new File("app:/config/updateConfig.xml");
	appUpdater.initialize(); 
	
}

public static function checkForUpdates(): void {
	appUpdater.checkNow();
}

private function onApplicationComplete(): void {
	LOG.info("onCreationComplete() invoked");
	NativeApplication.nativeApplication.addEventListener(Event.EXITING, onExiting);
	
	new SpringApplicationContext(onAppContextComplete).load();
}

private function onAppContextComplete(event: Event): void {
	LOG.debug("onAppContextComplete() invoked");
	
	MainServices.instance.dataVersionChecker.check();
	
	CoreModel.instance.playlists = MainServices.instance.persistenceService.fetchPlaylists();
	
}

private function onExiting(event: Event): void {
	LOG.debug("Shutdown hook running.");
	
	MainServices.instance.persistenceService.close();
}

private function onFinishedPlaying(event: PlayStateEvent): void {
	if((DisplayModel.instance.playItem is StoredItem) == false) {
		return;
	}
	const item: IPlayable = DisplayModel.instance.playItem;
	
	// const playlist: Playlist = Util.getParentPlaylist(item);
	const grid: PlaylistGrid = MainModel.instance.playlistGridHack;
	
	const newPlayItem: IPlayable = grid.getNextStoredItem(item);
	
	
	if(newPlayItem == null) {
		trace("-----------------------------------------------------");
		LOG.debug("Nothing to play next because new playitem is null.");
		trace("-----------------------------------------------------");
	} else {
		trace("-----------------------------------------------------");
		LOG.debug("Automatically playing next item: " + newPlayItem);
		trace("-----------------------------------------------------");
		new PlayItemEvent(newPlayItem).dispatch();
	}
}


private function onToggleFullscreen(event: ToggleFullscreenEvent): void {
	const stage: Stage = Application.application.stage;
	if(stage.displayState == StageDisplayState.NORMAL) {
		stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
		Application.application.currentState = 'stateFullscreen';
	} else {
		stage.displayState = StageDisplayState.NORMAL;
		Application.application.currentState = '';
	}
}